<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2016/10/26
 * Time: 18:52
 */



namespace SDXApi\Controller;
use Think\Controller;

set_time_limit(0);

class PublicController extends Controller
{

	/**
	 * Created by Jerry
	 */
    protected function get_891_orders()
    {
        $time = I('post.time', 0, 'trim');
        $page = I('post.page', 1, 'trim');
        $page += 0;
        $time += 0;



        if($page == 0){
            $page = 1;
        }

        if($time == 0){
            $time = gmtime();
            $gettime = date('Y-m-d', $time);
            $strtime = strtotime($gettime); //开始时间
        }
        else
        {
            $gettime = date('Y-m-d', $time);
            $strtime = strtotime($gettime) - 28800; //开始时间
        }



        $maxtime = strtotime('+1 days', $strtime);  //结束时间

        $orderInfoModel = D('Orderinfo');
        $count = $orderInfoModel->get_891_order_count($strtime, $maxtime);

        $pager = show_page($count, $page);

        $orderList = $orderInfoModel->get_891_order_info($strtime, $maxtime, $pager);


        if(empty($orderList)){
            returnApiError('暂无订单数据');
        }

        $data = array();
        foreach($orderList as $k=>$v){
            $data[$k] = $v;

            if($v['referer'] == '门店' || $v['referer'] == '891'){
                $data[$k]['referer'] = '线下';
            }else{
                $data[$k]['referer'] = '线上';
            }

            $data[$k]['goods_list'] = $this->get_order_goods_by_sn($v['order_id']);

            unset($data[$k]['order_id']);
        }

        $arr['order_list'] = $data;
        $arr['order_count'] = count($data);
        $arr['page'] = $pager;

        if(empty($arr)){
            returnApiError('暂无订单数据');
        }

        returnApiSuccess($arr);
    }

    /**
     * 返回退款数据
     * Created by Jerry
     */
	protected function get_891_order_by_refunds()
    {
        $time = I('post.time', 0, 'trim');
        $page = I('post.page', 1, 'trim');
        $page += 0;
        $time += 0;

		if($time == 0){
			$time = gmtime();
			$gettime = date('Y-m-d', $time);
			$strtime = strtotime($gettime); //开始时间
		}
		else
		{
			$gettime = date('Y-m-d', $time);
			$strtime = strtotime($gettime) - 28800; //开始时间
		}



		$maxtime = strtotime('+1 days', $strtime);  //结束时间

        $data = $this->get_refund_detail($strtime, $maxtime);

        $reData['refunds'] = $data;

        if(empty($data)){
            returnApiError('暂无退款订单数据');
        }

        returnApiSuccess($reData);
    }

	/**
	 * 商品信息返回
	 * Created by Jerry
	 */
	protected function get_goods_update()
	{
		$lastUpdateDate = I('post.lastUpdateDate', '');
		$sku = I('post.sku', 0, 'int');
		$barCode = I('post.barCode', '');
		$pageSize = I('post.pageSize', 20, 'int');
		$pageNo = I('post.pageNo', 1, 'int');

		$data = array();
		$goodsModel = D('Goods');

		if (empty($lastUpdateDate)) {
			$count = $goodsModel->findNoDeleteGoodsForAllCount($sku, $barCode);
			$page = show_page($count, $pageNo, $pageSize);

			$data = $goodsModel->findNoDeleteGoodsForAll($page, $sku, $barCode);
		}
		else
		{
			$startTime = strtotime($lastUpdateDate) - 28800;
			$count = $goodsModel->findNoDeleteGoodsByLastUpdateCount($startTime, $sku, $barCode);
			$page = show_page($count, $pageNo, $pageSize);

			$data = $goodsModel->findNoDeleteGoodsByLastUpdate($startTime, $page, $sku, $barCode);
		}

		$returnData = array();

		if (empty($data)) {
			$returnData = array(
				'status'		=>	false,
				'message'		=>	'',
				'errorMessage'	=>	'暂无商品数据',
				'totalCount'	=>	$count,
				'pageNo'		=>	$pageNo,
				'pageSize'		=>	count($data),
				'data'			=>	$data
			);

			returnApiError($returnData);
		}


		$data = $this->checkGoodsUpdate($data, $goodsModel);
		$returnData = array(
			'status'		=>	true,
			'message'		=>	'数据返回成功',
			'errorMessage'	=>	'',
			'totalCount'	=>	$count,
			'pageNo'		=>	$pageNo,
			'pageSize'		=>	count($data),
			'data'			=>	$data
		);

		returnApiSuccess($returnData);

	}

	/**
	 * 处理数据结构
	 * Created by Jerry
	 * @param $goods
	 * @return array|bool
	 */
	protected function checkGoodsUpdate($goods, $goodsModel)
	{
		if (empty($goods)) {
			return false;
		}

		$data = array();
		foreach ($goods as $key => $value) {
			if (!empty($value['original_img'])) {
				$data[$key]['imagePath'] = 'https://cdn.shidexian.com/' . $value['original_img'];
			}

			$data[$key]['productName'] = $value['goods_name'];
			$data[$key]['productDetailName'] = '';
			$data[$key]['sku']	= $value['goods_id'];
			$data[$key]['barCode'] = $value['goods_sn'];
			$data[$key]['productStatus'] = $value['is_on_sale'];
			$data[$key]['weight'] = $value['goods_weight'];
			$data[$key]['remark'] = $value['seller_note'];
			$data[$key]['countryName'] = $value['origin'];
			$data[$key]['categoryCode'] = $value['cat_id'];
			$data[$key]['categoryName'] = $value['cat_name'];
			$data[$key]['brandCode'] = $value['brand_id'];
			$data[$key]['brandName'] = $value['brand_name'];
			$data[$key]['productDesc'] = $value['goods_desc'];
			$data[$key]['tax'] = $value['tax_rate'];
			$data[$key]['salesPrice'] = $goodsModel->get_vip_price_for_all($value['goods_id'], 1);
			$data[$key]['vipPrice'] = $goodsModel->get_vip_price_for_all($value['goods_id'], 2);
			$data[$key]['lastUpdate'] = $value['last_update'];
			$data[$key]['model'] = $value['specs'];
			$data[$key]['sup_name'] = $value['sup_name'];
			$data[$key]['sup_id'] = $value['sup_id'];
		}

		return $data;
	}

    /**
     * 返回退款详情
     * Created by Jerry
     * @param $startTime
     * @param $endTime
     * @return mixed
     */
    private function get_refund_detail($startTime, $endTime)
    {
        $sql = "SELECT rl.refund_type, o.order_sn, r.goods_name, r.goods_sn, r.refund_number, r.refund_amount, r.opt_note,r.opt_time FROM sdx_refund_detail as r 
                RIGHT JOIN sdx_refund_log as rl ON rl.log_id = r.log_id
                RIGHT JOIN sdx_order_info as o ON r.order_id = o.order_id
                RIGHT JOIN sdx_goods as g ON g.goods_id = r.goods_id
                WHERE
                r.opt_time >= '{$startTime}'
                AND 
                r.opt_time < '{$endTime}'
                AND
                g.sources = '891'";

        $model =D();
        return $model->query($sql);
    }

    /**
     * 返回订单商品详情
     * Created by Jerry
     * @param $order_id
     * @return mixed
     */
    protected function get_order_goods_by_sn($order_id)
    {
        $orderGoodsModel =D();

        $sql = "SELECT 
                og.goods_name, og.goods_sn, og.goods_number, og.goods_price, og.goods_discount, og.discount_price
            FROM
                sdx_order_goods AS og
                    RIGHT JOIN
                sdx_goods AS g ON og.goods_id = g.goods_id
            WHERE
                og.order_id = '{$order_id}'
                    AND g.sources = '891'";
//        echo $sql;die;
        $data = $orderGoodsModel->query($sql);
        return $data;
    }
}